package com.asa.photopicker.photo;

import java.io.File;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedList;

import com.google.gson.annotations.SerializedName;
//import com.yunio.core.utils.LogUtils;
//import com.yunio.hsdoctor.entity.Folder.Media;
//import com.yunio.hsdoctor.util.FileUtil;
//import com.yunio.hsdoctor.util.Utils;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

public class Folder implements Comparator<Folder> {

	private final String TAG = "Folder";

	private String path;
	@SerializedName("child_path")
	private String childPath;
	private ArrayList<Media> childList;

	public ArrayList<Media> getChildList() {
		return childList;
	}

	public Folder(String path) {
		this.path = path;
	}

	public void setThumb(String path) {
		this.childPath = path;
	}

	public boolean contains(Media media) {
		if (media == null) {
			return false;
		}
		if (Utils.isEmpty(childList)) {
			return false;
		}
		return childList.contains(media);
	}

	public int indexOf(Media media) {
		if (media == null) {
			return -1;
		}
		return childList.indexOf(media);
	}

	public void set(int index, Media media) {
		childList.set(index, media);
	}

	public boolean add(String path) {
		if (TextUtils.isEmpty(path)) {
			return false;
		}
		if (childList == null) {
			childList = new ArrayList<Media>();
		}
		if (TextUtils.isEmpty(childPath)) {
			childPath = path;
		}
		Media media = new Media();
		media.setPath(path);
		return childList.add(media);
	}

	public boolean add(LinkedList<Media> list) {
		if (Utils.isEmpty(list)) {
			return false;
		}
		if (childList == null) {
			childList = new ArrayList<Media>();
		}
		return childList.addAll(list);
	}

	public Media get(int position) {
		if (position >= getCount()) {
			return null;
		}
		return childList.get(position);
	}

	public String getName() {
		if (TextUtils.isEmpty(path)) {
			return null;
		}
		return Utils.getFileName(path);
	}

	public String getPath() {
		return path;
	}

	public String getChild() {
		return childPath;
	}

	public int getCount() {
		if (Utils.isEmpty(childList)) {
			return 0;
		}
		return childList.size();
	}

	public void init() {
		if (childList == null) {
			return;
		}
		for (Media media : childList) {
			media.checked = false;
		}
	}

	public boolean valid() {
		if (TextUtils.isEmpty(path) || TextUtils.isEmpty(childPath)) {
			return false;
		}
		try {
			File curFile = new File(path);
			if (curFile == null || curFile.isHidden() || curFile.isFile()) {
				return false;
			}
			File childFile = new File(childPath);
			if (childFile == null || childFile.isHidden() || childFile.isDirectory()) {
				return false;
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public ArrayList<String> getChecked() {
		ArrayList<String> list = new ArrayList<String>();
		for (Media media : childList) {
			if (media.isChecked()) {
				list.add(media.path);
			}
		}
		return list;
	}

	public void clearChecked() {
		for (Media media : childList) {
			if (media.isChecked()) {
				media.setChecked(false);
			}
		}
	}

	@Override
	public int compare(Folder lhs, Folder rhs) {
		try {
			return lhs.path.compareToIgnoreCase(rhs.path);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public String toString() {
		return "Folder [TAG=" + TAG + ", path=" + path + ", childPath=" + childPath + ", childList=" + childList + "]";
	}

	public static class Media implements Parcelable {
		private String path;
		private boolean checked;

		public Media() {

		}

		public Media(Parcel parcel) {
			path = parcel.readString();
			checked = parcel.readInt() == 1;
		}

		public String getPath() {
			return path;
		}

		public void setPath(String path) {
			this.path = path;
		}

		public boolean isChecked() {
			return checked;
		}

		public void setChecked(boolean checked) {
			this.checked = checked;
		}

		public void check() {
			if (TextUtils.isEmpty(path)) {
				return;
			}
			this.checked = !checked;
		}

		public boolean isVideo() {
			if (TextUtils.isEmpty(path)) {
				return false;
			}
			return path.endsWith(".mp4");
		}

		@Override
		public boolean equals(Object o) {
			if (o instanceof Media) {
				return path.equals(((Media) o).getPath());
			}
			return super.equals(o);
		}

		@Override
		public int describeContents() {
			return 0;
		}

		public final static Parcelable.Creator<Media> CREATOR = new Creator<Folder.Media>() {

			@Override
			public Media[] newArray(int size) {
				return new Media[size];
			}

			@Override
			public Media createFromParcel(Parcel source) {
				return new Media(source);
			}
		};

		@Override
		public void writeToParcel(Parcel dest, int flags) {
			dest.writeString(path);
			dest.writeInt(checked ? 1 : 0);
		}

	}

}
